<?php
/**
 * 留言管理
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         MessageController.php
 */
namespace Scshux\Backend\Controllers;
use Scshux\Mvc\SModel;
use Scshux\Mvc\SQuery;
use Scshux\Utils\Tree;
use Scshux\Utils\Time;
use Scshux\Utils\Uploader;
use Scshux\Utils\Variable;
class MessageController extends  ControllerBase
{

	/**
	 * 文章列表
	 */
	public  function indexAction()
	{
		$is_ajax  = $this->request->get('is_ajax','int',0);
		$act  = $this->request->get('act');
		if($act == 'remove'){
			$this->_remove();
		}
		$dataList = $this->_getList();
		$this->view->setVar('dataList', $dataList);
		if($is_ajax){

			$this->view->setVar('full_page',0);
			$this->view->start();
			$this->view->render('message','index');
			$this->view->finish();
			$content = $this->view->getContent();
			$data = array(
					'content' => $content,
					'filter'  => $dataList->filter,
					'totalPage' => $dataList->totalPage,
					'recordCount'=>$dataList->totalSize,
					'currentPage'=>$dataList->currentPage,
					'extended'   =>$dataList->extended
			);

			$this->sendResult(true,'',$data);
		}else{
			$this->view->setVar('full_page',1);
		}
	}

	
	/**
	 * 装修咨询
	 */
	public function zixunAction()
	{
	    $is_ajax  = $this->request->get('is_ajax','int',0);
	    $act  = $this->request->get('act');
	    if($act == 'remove'){
	        $this->_removeZixun();
	    }
	    $dataList = $this->_getZixunList();
	    $this->view->setVar('dataList', $dataList);
	    if($is_ajax){
	    
	        $this->view->setVar('full_page',0);
	        $this->view->start();
	        $this->view->render('message','zixun');
	        $this->view->finish();
	        $content = $this->view->getContent();
	        $data = array(
	                'content' => $content,
	                'filter'  => $dataList->filter,
	                'totalPage' => $dataList->totalPage,
	                'recordCount'=>$dataList->totalSize,
	                'currentPage'=>$dataList->currentPage
	        );
	        $this->sendResult(true,'',$data);
	    }else{
	        $this->view->setVar('full_page',1);
	    }
	}
	
	
	/**
	 * 删除信息
	 */
	protected  function _remove()
	{
		$id = $this->request->get('id','int',0);
		$messageModel = new SModel('message');
		$messageModel->del('id='.$id);
	}

	
	/**
	 * 删除咨询
	 */
	private function _removeZixun()
	{
	    $id = $this->request->get('id','int',0);
	    $messageModel = new SModel('zxzixun');
	    $messageModel->del('id='.$id);
	}


  /**
   * 获取用户信息
   */
   protected  function _getList()
   {
	   	$filter = array();

	   	$page = $this->request->get('page','int',1);

	   	$where = '1=1';
	   	
	   	//新增语言过滤
	   	$site_id = $this->getCore()->getSiteID();
	   	$where .= ' and m.site_id = '.$site_id;
	   	
	   	if(!empty($_REQUEST['keywords'])){
	   		$where .= ' and (m.`nickname` like "%'.$_REQUEST['keywords'].'%"'.
	   		    ' or m.phone like "%'.$_REQUEST['keywords'].'%")';
	   		$filter['keywords'] = $_REQUEST['keywords'];
	   	}

       	if($_REQUEST['start_time']){
    		$where .= " and m.created>=".strtotime($_REQUEST['start_time'].' 00:00:00');
    		$filter['start_time'] = $_REQUEST['start_time'];
    	}

    	if($_REQUEST['end_time']){
    		$where .= " and m.created<=".strtotime($_REQUEST['end_time'].' 23:59:59');
    		$filter['end_time'] = $_REQUEST['end_time'];
    	}

	   	$helpQuery = new SQuery('message as m');
	   	$helpQuery->fields = 'm.*';
	   	$helpQuery->page =  $page;
	   	$helpQuery->where = $where;
	   	$helpQuery->order = 'm.id desc';
	   	$helpQuery->pagesize = 12;

	   	$items = $helpQuery->find();
		//业务   面积的处理
		$items = $this->format($items);
		
	   	$dataList = new \stdClass();
	   	$dataList->totalPage = $helpQuery->paging->getTotalPage();
	   	$dataList->currentPage = $helpQuery->paging->getIndex();
	   	$dataList->items = $items;
	   	$dataList->totalSize = $helpQuery->paging->getTotalSize();
	   	$dataList->filter = $filter;

	   	return  $dataList;
   }
   
   /**d
    * 业务面积的格式处理
    */
   private function format($ob){
   		if(!empty($ob))
   		{
   			$ywtype = Variable::getYwClass();
   			$ywsize = Variable::getYwSize();
   			 
   			foreach($ob as $vo)
   			{
   				$tmyw = explode(',', $vo->ywtype);
   				if($tmyw)
   				{
   					foreach ($tmyw as $ty)
   					{
   						$vo->ywstr .= $ywtype[$ty].'|' ;
   					}
   				}
   					
   				$tmmj = explode(',', $vo->size);
   				if($tmmj)
   				{
   					foreach ($tmmj as $mj)
   					{
   						$vo->mjstr .= $ywsize[$mj].'|' ;
   					}
   				}
   				$vo->ywstr = rtrim($vo->ywstr,'|');
   				$vo->mjstr = rtrim($vo->mjstr,'|');
   			}
   		}
   		
		return $ob;
   }
   /**
    * 咨询列表
    */
   private function _getZixunList()
   {
       $filter = array();
       
       $page = $this->request->get('page','int',1);
       
       $where = '1=1';
       
       
       $helpQuery = new SQuery('zxzixun');
       $helpQuery->fields = '*';
       $helpQuery->page =  $page;
       $helpQuery->where = $where;
       $helpQuery->order = 'id desc';
       $helpQuery->pagesize = 12;
       
       $items = $helpQuery->find();
       $dataList = new \stdClass();
       $dataList->totalPage = $helpQuery->paging->getTotalPage();
       $dataList->currentPage = $helpQuery->paging->getIndex();
       $dataList->items = $items;
       $dataList->totalSize = $helpQuery->paging->getTotalSize();
       $dataList->filter = $filter;
       
       return  $dataList;
   }
   
}